package vader.config.sms.config;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.teaopenapi.models.Config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * @author yongxin
 * @Title:
 * @Package
 * @Description:
 * @date 2021/3/1517:06
 * 在需要使用的模块引入配置
 * test参考 oms的OmsOrderServiceImplTest
 * fancy:
 *   sms:
 *     enable: true
 *     accessKeyId: LTAI564g10IBEdVi
 *     accessKeySecret: Gl5LYfTiA5oFAqbCfPH4wqZCY53ZMQ
 *     signName: FancyApp
 */
@Configuration
@ConditionalOnProperty(name = "fancy.sms.enable", havingValue = "true")
@Slf4j
public class AliyunSmsConfig {

    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId = "";
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret = "";
    @Value("${fancy.sms.signName}")
    public String signName = "Fancy翻牌";

    //endpoint固定
    private String endpoint = "dysmsapi.aliyuncs.com";

    private Client smsClient;
    /**
     * 初始化短信客户端信息
     */
    @PostConstruct
    public void initSmsClient() throws Exception {
        Config config = new Config();
        config.accessKeyId = accessKeyId;
        config.accessKeySecret = accessKeySecret;
        config.endpoint = endpoint;
        smsClient = new Client(config);
    }

    /**
     *获取短信客户端信息
     * @return
     */
    public Client getSmsClient(){
        if (smsClient == null){
            log.error("init aliyun sms error");
        }
        return smsClient;
    }
}
